package com.controller.system;

import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.utils.EntityConversionUtil;
import com.github.pagehelper.PageInfo;
import com.model.base.PageResult;
import com.model.base.Result;
import com.model.domain.system.GetSystemDeviceModelInfoDO;
import com.model.domain.system.GetSystemDomainInfoDO;
import com.model.dto.system.deviceModel.GetModelListDTO;
import com.model.dto.system.deviceModel.SaveModelInfoDTO;
import com.model.dto.system.domain.DeleteDomainInfoDTO;
import com.model.dto.system.domain.GetDomainListDTO;
import com.model.dto.system.domain.SaveDomainInfoDTO;
import com.model.pojo.system.SystemDeviceModelInfo;
import com.model.pojo.system.SystemDomainInfo;
import com.model.vo.system.deviceModel.getModelList.GetModelListVO;
import com.model.vo.system.domain.getDomainList.GetDomainListVO;
import com.service.device.DeviceObdBaseInfoService;
import com.service.system.SystemDeviceModelInfoService;
import com.service.system.SystemDomainInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.Get;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * TODO
 *
 * @Description
 * @Author tm
 * @Date 2023-11-22 11:06
 **/
@Api(tags = {"终端型号管理"})
@Slf4j
@RestController
@RequestMapping("/v1/system/deviceModel")
@RequiredArgsConstructor
public class SystemDeviceModelInfoController {
    private final SystemDeviceModelInfoService systemDeviceModelInfoService;
    private final DeviceObdBaseInfoService deviceObdBaseInfoService;
    @Log(title = "终端型号管理-终端型号列表", businessType = BusinessType.SELECT)
    @ApiOperation(value = "终端型号列表")
    @PostMapping("/getModelList")
    public Result<PageResult<List<GetModelListVO>>> getModelList(@RequestBody @Valid GetModelListDTO dto){
        List<GetModelListVO> voList = new ArrayList<GetModelListVO>();
        GetSystemDeviceModelInfoDO getSystemDeviceModelInfoDO = EntityConversionUtil.copy(dto, GetSystemDeviceModelInfoDO.class);
        getSystemDeviceModelInfoDO.setSortRow("createTime");
        List<SystemDeviceModelInfo> list = systemDeviceModelInfoService.getPageList(getSystemDeviceModelInfoDO);
        if(list!=null && list.size()>0){
            voList = EntityConversionUtil.copyList(list,GetModelListVO.class);
        }
        PageInfo<SystemDeviceModelInfo> pageInfo = new PageInfo<>(list);
        PageResult<List<GetModelListVO>> build = PageResult.<List<GetModelListVO>>builder().result(voList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }

    @Log(title = "终端型号管理-保存域名", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "保存域名")
    @PostMapping("/saveModelInfo")
    public Result<Objects> saveModelInfo(@RequestBody @Valid SaveModelInfoDTO dto){
        int num = 0;
        if(dto.getId()!=null && dto.getId()!=0){
            SystemDeviceModelInfo info = systemDeviceModelInfoService.getById(dto.getId());
            if(info == null){
                return Result.fail("9999","当前记录不存在");
            }
            info.setName(dto.getName());
            info.setRemark(dto.getRemark());
            info.setType(dto.getType());
            num = systemDeviceModelInfoService.update(info);
        }else{
            SystemDeviceModelInfo info = EntityConversionUtil.copy(dto,SystemDeviceModelInfo.class);
            num = systemDeviceModelInfoService.save(info);
        }
        if(num>0){
            return Result.suc();
        }
        return Result.fail("9999","保存失败");
    }
}
